package com.gitee.ywj1352.算法.week01;

import java.util.PriorityQueue;

public class 反转链表 {

    public static ListNode reverseBetween(ListNode head, int left, int right) {
        //pre 1-2
        // ListNode pre = head;
        ListNode returnHead = head;
        //current 5-4-3
        ListNode current = null;
        //tail 6-7-8
        ListNode tailHead = head;
        int tail = right;
        for(int i= 0; i < tail; i ++){
            if(i == left-2){
                current = tailHead;
            }
            tailHead = tailHead.next;
        }
        ListNode tailNode = current.next ;
        int end  = right - left;
        ListNode preR = null,currentR = current.next;
        while(currentR !=null && end > 0){
            ListNode next = currentR.next;
            currentR.next = preR;
            preR= currentR;
            //currentR = 6-7-8
            currentR = next;
            end --;
            PriorityQueue he = new PriorityQueue<>();

        }
        current.next = preR;
        tailNode.next = currentR;
        return returnHead;
    }


    public static void main(String[] args) {

        ListNode l1 = new ListNode(1);
        ListNode l2 = new ListNode(2);
        ListNode l3 = new ListNode(3);
        ListNode l4 = new ListNode(4);
        ListNode l5 = new ListNode(5);
        ListNode l6 = new ListNode(6);
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;
        l5.next = l6;

        ListNode node = reverseBetween(l1,2,5);

       print(node);
    }

    public static void print(ListNode node){
        while (node!=null){
            System.out.println(node.val);
            node = node.next;
        }

    }
}
